# Copyright (c) 2012 Nathan Grubb
# Available under the new BSD license; see LICENSE.txt

"""plugin/login.py: The login plugin handles user login state information for the 
   bot.
"""

class Login(object):
    type = "command"
    commands = {"login": 2,}
    permissions = None
    name = "Login"
    desc = "Handles user login"
    
    def __init__(self, api, msg):
        self.api = api
        self.msg = msg

    def command_login(self, args):
        usern, passw = args

        valid = False
        # Since OPS and ADMINS are automatically merged, just cycle OPS
        # to determine login detail validity
        for username, password in self.api.grubbot.config.values["ops"].items():
            if username == usern and password == passw:
                valid = True
                break

        type = None
        if valid:
            # Determine if user is an admin; if not, they are an op
            if type == None:
                type = "op"
            else:
                for username in self.api.grubbot.config.values["admins"]:
                    if username == usern:
                        type = "admin"
                        break

            self.api.grubbot.sessions[usern] = type            
            
        if valid:
            self.api.reply(self.msg, "Logged in successfully.")
        else:
            self.api.reply(self.msg, "Login failed.")

plugin = Login